local fengyang = fk.CreateSkill{
    name = "ld__fengyang",
    tags = { Skill.Compulsory },
}

local H = require "packages.ol_hegemony.util"

fengyang:addEffect("arraysummon", {
    array_type = "formation",
})

fengyang:addEffect(fk.BeforeCardsMove,{
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(fengyang.name) or #player.room.alive_players < 4  then return false end
    local room = player.room
    for _, move in ipairs(data) do
      if move.from and H.inFormationRelation(player, move.from)and player:hasShownSkill(fengyang.name) and
        (move.moveReason == fk.ReasonDiscard or (move.toArea == Card.PlayerHand and move.to ~= move.from and move.moveReason ~= fk.ReasonGive))
        and move.proposer and not H.compareKingdomWith(move.proposer, player)  then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids, targets = {}, {}
    room:notifySkillInvoked(player, fengyang.name, "defensive")
    player:broadcastSkillInvoke(fengyang.name)
    for _, move in ipairs(data) do
      if move.from and H.inFormationRelation(player, move.from) and
        (move.moveReason == fk.ReasonDiscard or (move.toArea == Card.PlayerHand and move.to ~= move.from and move.moveReason ~= fk.ReasonGive))
        and move.proposer and not H.compareKingdomWith(move.proposer, player) then
        local move_info = {}
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if info.fromArea == Card.PlayerEquip then
            table.insert(ids, id)
          else
            table.insert(move_info, info)
          end
          if #ids > 0 then
            table.insertIfNeed(targets, move.from)
            move.moveInfo = move_info
          end
        end
      end
    end
    if #ids > 0 then
      room:doIndicate(player.id, targets)
      room:sendLog{
        type = "#cancelDismantle",
        card = ids,
        arg = fengyang.name,
      }
    end
  end,
})

--阵法技提示
local fengyang_spec = {
  can_refresh = function(self, event, target, player, data)
      return player:hasShownSkill(fengyang.name, true, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      local ret = #room.alive_players > 3 and player:hasSkill(fengyang.name) and H.inFormationRelation(p, player)
       if ret and player:hasShownSkill(fengyang.name, true, true) then
        room:setPlayerMark(p, "@@fengyang", 1)
      else
        room:setPlayerMark(p, "@@fengyang", 0)
      end
    end
  end,
}

fengyang:addEffect(fk.TurnStart, fengyang_spec)
fengyang:addEffect(fk.GeneralRevealed, fengyang_spec)
fengyang:addEffect(fk.EventAcquireSkill, fengyang_spec)
fengyang:addEffect(fk.GeneralHidden, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = fengyang_spec.on_refresh,
})
fengyang:addEffect(H.GeneralRemoved, fengyang_spec)
fengyang:addEffect(H.PlayerRemoved, fengyang_spec)
fengyang:addEffect(fk.Deathed, fengyang_spec)
--失去技能
fengyang:addLoseEffect(function (self, player, is_death)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("@@fengyang") > 0 then
      room:setPlayerMark(p, "@@fengyang", 0)
      end
    end
  end)

Fk:loadTranslationTable{
    ["ld__fengyang"] = "风扬",
    [":ld__fengyang"] = "<a href='heg_formation_skill'>阵法技</a>，与你处于同一<a href='heg_formation'>队列</a>的角色装备区内的牌被与你势力不同的角色弃置或获得时，取消之。",

    ["@@fengyang"] = "风扬",

    ["$ld__fengyang1"] = "谁也休想染指江东寸土。",
    ["$ld__fengyang2"] = "如此咽喉要地，吾当倾力守之。",
}

return fengyang